var iconv = require('iconv-lite');
const jsdom = require("jsdom");
const fs = require("fs");
const {JSDOM} = jsdom;
const {Script} = require("vm");

var content = fs.readFileSync('guangdong.html', {encoding: 'binary'});
var buf = new Buffer.from(content, 'binary');
var colorado_html = iconv.decode(buf, 'utf-8');

plugins = [];
var options = {
    // referrer: "",
    userAgent: "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
    resources: "usable",    // 假如你想通过<script src="">来执行外部脚本，你需要确保已经加载了它们。为此，请添加选项resources:"usable"
    runScripts: "dangerously",  //要在页面内启用执行脚本
    url: 'http://www.cbex.com.cn/',
    // virtualConsole: new jsdom.VirtualConsole(),
    cookieJar: new jsdom.CookieJar(),
    pretendToBeVisual: true,    //jsdom会假装它正在呈现和显示内容
    beforeParse(window) {
        // window.document.addEventListener = (a, b, c) => {
        //     console.log('addEventListener a', a);
        //     console.log('addEventListener b', b);
        //     // return b()
        // };
        window.screen = {
            width: 2560,
            availHeight: 1400,
            availLeft: 0,
            availTop: 0,
            availWidth: 2560,
            colorDepth: 24,
            height: 1440,
            pixelDepth: 24,
            orientation: {
                angle: 0,
                type: "landscape-primary"
            }
        };
        window.getjs = () => {
            return window.js
        };
        window.navigator.languages = ["zh-CN", "zh"];
        window.navigator.plugins = plugins;
        window.navigator.getBattery = () => {
            return {
                'charging': true,
                'chargingTime': 0,
                'dischargingTime': Infinity,
                'level': 1,
            }
        };
        window.navigator.mimeTypes = [{"type": "application/360softmgrplugin"}, {"type": "application/aliedit"}, {"type": "application/asx"}, {"type": "application/bd-npyunwebdetect-plugin"}, {"type": "application/cenroll.cenroll.version.1"}, {"type": "application/futuresplash"}, {"type": "application/futuresplash"}, {"type": "application/futuresplash"}, {"type": "application/futuresplash"}, {"type": "application/hwepass2001.installepass2001"}, {"type": "application/hwpta.itrushwpta"}, {"type": "application/hwwdkey.installwdkey"}, {"type": "application/itrusenroll.certenroll.version.1"}, {"type": "application/mozilla-npqihooquicklogin"}, {"type": "application/npalicdo"}, {"type": "application/npalissologin"}, {"type": "application/npqqwebgame"}, {"type": "application/nptxsso"}, {"type": "application/npxf-qqminidl"}, {"type": "application/npxluser_plugin"}, {"type": "application/pdf"}, {"type": "application/pta.itruspta.version.1"}, {"type": "application/qscall-plugin"}]
        window.navigator.connection = {
            'downlink': 10,
            'effectiveType': '4g',
            'rtt': 50,
            'saveData': false
        };
        window.Infinity = Infinity;
    },

};


const dom = (new JSDOM(colorado_html, options));
const window = dom.window;

window.addEventListener('load', function () {
    console.log("ready to roll!");
    // console.log(window.document.getElementById('project-table-box').innerHTML)

    console.log(dom.window.document.documentElement.outerHTML);

});